#include "TestCase.h"

namespace AllTing
{

namespace Test
{

TestCase::~TestCase()
{
}

UInt32 TestCase::Size() const
{
	return (UInt32)vTests_.size();
}

void TestCase::Add(Testable* pTest)
{
	assert(pTest);

	vTests_.push_back(pTest);
}

TestIterator TestCase::Remove(Testable *pTest)
{
	assert(pTest);

	TestIterator itStart = vTests_.begin();
	TestIterator itEnd = vTests_.end();
	for(;itStart!=itEnd;++itStart)
	{
		if((*itStart) == pTest)
		{
			vTests_.erase(itStart);
			return itStart;
		}
	}

	return vTests_.end();
}

const Testable* TestCase::At(UInt32 nIndex)
{
	assert(nIndex < vTests_.size());

	if(vTests_.empty())
		return Null;

	UInt32 size = Size();
	if(size<=nIndex)
		return Null;

	return vTests_.at(nIndex);
}

Bool TestCase::Run()
{
	Bool bSuccess = True;
	TestIterator itStart = vTests_.begin();
	TestIterator itEnd = vTests_.end();

	for( ; itStart!=itEnd; ++itStart)
	{
		Bool bStopOnFailed = (*itStart)->StopOnFailed();

		if(False == SetUp() && bStopOnFailed)
		{
			bSuccess = False;
			break;
		}
		
		if(False == (*itStart)->Run() && bStopOnFailed)
		{
			bSuccess = False;
			break;
		}

		if(False == TearDown() && bStopOnFailed)
		{
			bSuccess = False;
			break;
		}
	}

	return bSuccess;
}

}	// namespace Test

}	// namespace AllTing